
# POST

# https://fanyi.baidu.com/sug

import urllib.request
import urllib.parse
import json

url = "https://fanyi.baidu.com/sug"

headers = {
    'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'
}

# 参数
data = { "kw":"admin" }

# 编解码
datacode = urllib.parse.urlencode(data).encode("utf-8")
# 请求参数 required parameter
require_param =  urllib.request.Request(url=url,headers=headers,data=datacode)
# 模拟向服务器发送请求
response = urllib.request.urlopen(require_param)

print(response)
# <http.client.HTTPResponse object at 0x000001C2C5AA2BC0>

# 获取响应数据
content = response.read().decode("utf-8")
# print(content)

# {"errno":0,"data":[{"k":"admin","v":"abbr. \u7ba1\u7406\uff0c\u884c\u653f"},{"k":"Admin","v":"abbr. administration \u7ba1\u7406\uff0c\u7ecf\u8425\uff0c\u884c\u653f\u90e8\u95e8"},{"k":"administer","v":"vt. \u7ba1\u7406; \u6267\u884c; \u7ed9\u4e88; \u6cbb\u7406\uff08\u56fd\u5bb6\uff09 vi. \u6267\u884c\u9057\u4ea7\u7ba1\u7406\u4eba\u7684\u804c\u8d23; \u7ed9\u4e88\u5e2e\u52a9; \u62c5\u5f53\u7ba1\u7406\u4eba"},{"k":"adminicular","v":"adj. \u8865\u5145\u7684\uff0c\u8f85\u52a9\u7684"},{"k":"adminiculum","v":"[\u533b] \u652f\u5ea7"}],"logid":2135871559}

print(type(content))

# 字符串转json
obj = json.loads(content)
print(obj)


